package edu.zut.main.thread;

public class DeadLock {
    public static void main(String[] args) {

        People boy = new People("boy");
        People girl = new People("girl");

        new Thread(new DeadThread(boy)).start();
        new Thread(new DeadThread(girl)).start();


    }
}

//刀叉问题
class DeadThread implements Runnable{
    private People people;

    public DeadThread(People people) {
        this.people = people;
    }

    @Override
    public void run() {

        people.EatFood();

    }
}

class Knife{}
class Fork{}
class People{
    private Knife knife = new Knife();
    private Fork fork = new Fork();
    private String name;

    public People(String name) {
        this.name = name;
    }
    public void EatFood(){
        if (name.equals("boy")) {
            synchronized (knife) {
                System.out.println(name+"把刀子拿走了");
                synchronized (fork){
                    System.out.println(name+"又把叉子拿走了");
                }
            }

        }
        if (name.equals("girl")) {
            synchronized (fork) {
                System.out.println(name+"把叉子拿走了");
                synchronized (knife){
                    System.out.println(name+"又把刀子拿走了");
                }
            }

        }

    }


}